
// 检查密码是否为空
export const validatePass = (rule, value, callback) => {
    
    if (value === '') {
        callback('请输入密码');
    } else {
        callback();
    }
};

/**
 * 校验新密码不能和旧密码一样
 * @param  {Object} rule     {传入的数据对象}
 * @param  {Object} value    {需要校验的value值}
 * @param  {function} callback {判断之后执行的回调}
 * @return {undefined}
 */
export const validateNewPwd = (rule, value, callback) => {
    
    if (value === '') {
        callback('请输入密码');
    } else {
        if (rule.pwd_info.formValidate.old_password === rule.pwd_info.formValidate.new_password) {
            callback('新旧密码不能一样');
        }else {
            callback();
        }
    }
};

// 检查密码是否一致
export const validatePassCheck = function (rule, value, callback) {
    if (value === '') {
        callback('请确认您的密码!');
    } else {
        if (rule.pwd_info.formValidate.new_password !== rule.pwd_info.formValidate.confirm_password) {
            callback('两次输入密码不一致！');
        }else {
            callback();
        }
    }
};

// 检查电话号码格式（包括手机和座机）
export const validatePhone = ( rule, value, callback ) => {
    if( value === '' ){
        callback(new Error('请输入手机号'));
    }else if( !/^1[356789]\d{9}$/.test(value) ){
        if( !/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(value) ){
            callback(new Error('手机格式不正确'));
        }else {
            callback();
        }
        
    }else{
        callback();
    }
};

//检查是否是数字
export const validateNumber = ( rule, value, callback ) => {
    
    if( value === '' ) {
        callback('必填项');
    }else if( !Number(value) && Number(value) !== 0 ){
        callback('必须是数字');
    }else {
        if( rule.maxValue ) {

            if( parseInt(value) > rule.maxValue ) {

                callback('超过最大限制值');

            }else {

                callback();

            }

        }else {

            callback();

        }
        
    }

};

//检查是否选择了图片
export const validateFile = ( rule, file, callback ) => {

    let max_size;

    if(rule.maxSize){

        max_size = rule.maxSize;

    }else {

        max_size = 1536;

    }

    if(! file || typeof file !== 'object'){

        callback('还未选择图片！');

    }else {

        let type = file.name.split('.')[1];

        let name = file.name.split('.')[0];

        let ch_reg = new RegExp("[\\u4E00-\\u9FFF]+","g");

        if('jpg jpeg png'.split(' ').indexOf(type) === -1){

            callback('只能选择文件后缀为jpg,jpeg,png的图片！');

        }else if( parseFloat(file.size)/1024 > max_size ){

            callback('图片大小不能超过' + max_size/1024 + 'M！');

        }else {

            callback();

        }

    }
    
};

//检查是否选择了图片
export const validateImage = ( rule, file, callback ) => {

    if(! file || typeof file !== 'object'){

        callback('请选择图片！');

    } else {

        callback();

    }
    
};

/**
 * @function validateFile
 * @param  {Object} rule     {可传递的参数对象}
 * @param  {Object} file     {file文件对象}
 * @param  {function} callback {校验的回调函数}
 * @return {undefined} 
 */
export const validateUpdateFile = ( rule, file, callback ) => {

    let max_size;

    if(rule.maxSize){

        max_size = rule.maxSize;

    }else {

        max_size = 1536;

    }

    if( file && typeof file === 'object' ) {

        let type = file.name.split('.')[1];

        if('jpg jpeg png'.split(' ').indexOf(type) === -1){

            callback('只能选择文件后缀为jpg,jpeg,png的图片！');

        }else if( parseFloat(file.size)/1024 > max_size ){

            callback('图片大小不能超过' + max_size/1024 + 'M！');

        }else if( ch_reg.test(name) ){

            callback('图片名字不能包含中文！');

        }else {

            callback();

        }

    }else {

        callback();

    }
    
};

// 检查城市是否为空
export const validateCityCheck =  ( rule, value, callback ) => {
    if( value.length === 0 ){
        callback('城市不能为空');
    }else{
        callback();
    }
};

// 检查多选是否为空
export const validateMulti =  ( rule, value, callback ) => {
    if( value.length === 0 ){
        callback('必选');
    }else{
        callback();
    }
};

/**
 * 校验是否选择了分期数以及各个分期数是否已经正确填写
 * @param  {Object} rule     {可传递的参数对象}
 * @param  {array} value    {多选框的value}
 * @param  {function} callback {校验的回调函数}
 * @return {undefined}
 */
export const validateStageDivide = ( rule, value, callback ) => {

    if( value.length === 0 ) {

        callback('必选');

    }else {

        let error = '';

        value.some((key)=>{

            if( !rule.form_data.formValidate.platform_division_percent_stage[key] ||
                !rule.form_data.formValidate.agent_division_percent_stage[key] ||
                !rule.form_data.formValidate.refund_percent_stage[key]  ) {

                error += rule.form_data.stage_map[key] + '期存在未填信息';

            }else {

                if( (!Number(rule.form_data.formValidate.platform_division_percent_stage[key]) && 
                    Number(rule.form_data.formValidate.platform_division_percent_stage[key]) !== 0) ||
                    (!Number(rule.form_data.formValidate.agent_division_percent_stage[key]) && 
                    Number(rule.form_data.formValidate.agent_division_percent_stage[key]) !== 0) ||
                    (!Number(rule.form_data.formValidate.refund_percent_stage[key]) && 
                    Number(rule.form_data.formValidate.refund_percent_stage[key]) !== 0) ) {

                    error += rule.form_data.stage_map[key] + '期必须是数字';

                }

            }

        });

        callback(error);

    }
    
}

/**
 * 校验是否选择了分期数以及各个分期数是否已经正确填写(代理商权限)
 * @param  {Object} rule     {可传递的参数对象}
 * @param  {array} value    {多选框的value}
 * @param  {function} callback {校验的回调函数}
 * @return {undefined}
 */
export const validateCustStageDivide = ( rule, value, callback ) => {
    
    if( value.length === 0 ) {

        callback('必选');

    }else {

        let error = '';

        value.some((key)=>{

            if( !rule.form_data.formValidate.agent_division_percent_stage[key] ) {

                error += rule.form_data.stage_map[key] + '期存在未填信息';

            }else {

                if( !Number(rule.form_data.formValidate.agent_division_percent_stage[key]) && 
                    (Number(rule.form_data.formValidate.agent_division_percent_stage[key]) !== 0) ) {

                    error += rule.form_data.stage_map[key] + '期必须是数字';

                }

            }

        });

        callback(error);

    }
    

}